<?php
/**
 * 获取事业部所有的部门，按城市归类
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Hr_OutInterface");
apf_require_class("Bll_Report_Report");
apf_require_class("Bll_Hr_Property");
class Ajax_GetBuDepartmentController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $data = array();
        if(empty($params['user_id'])){
            $hroa_bll = new Bll_Hr_OutInterface();
            $res = $hroa_bll->get_department_by_business();
            if(!empty($res)){
                foreach($res as $val){
                    $data[$val['bu_id']][$val['city_id']][]=array('department_id'=>$val['function_id'].'-'.$val['bu_id'].'-'.$val['department_id'].'-'.$val['city_id'].'-'.$val['department_name'],'department_name'=>$val['department_name']);
                }
            }
        }elseif($params['user_id']){
            $report_bll = new Bll_Report_Report();
            $bu_list = Bll_Hr_Property::get_business_list();
            if(empty($params['id'])){
                $list = $report_bll->get_department_by_uid($params['user_id']);
                if(!empty($list)){
                    foreach($list as $val){
                        $data[$val['bu_id']][$val['id']] = $val['department_name'];//事业部下的部门
                        $data['name_list'][$val['bu_id']] = $bu_list[$val['bu_id']]['bu_name'];
                    }
                }
            }else{//delete
                $ids = explode(',',$params['id']);
                $report_bll->delete_role($ids);
                $data = array();
            }
        }
        $this->json_encode(1,$data);
    }
}